package com.biz.primus.ms.tms.dao.repository;

import com.biz.primus.model.tms.enums.DeliveryOrderStatus;
import com.biz.primus.ms.base.jpa.repository.CommonJpaRepository;
import com.biz.primus.ms.tms.dao.po.DeliveryOrderPo;
import org.springframework.data.jpa.repository.Query;

import java.util.Collection;
import java.util.List;

/**
 * DeliverOrderRepository
 *
 * @author JKLiues
 * @date 2017年11月21日
 */
public interface DeliverOrderRepository extends CommonJpaRepository<DeliveryOrderPo, Long>, DeliverOrderDao {

    /**
     * 通过配送单号查配送单
     *
     * @param deliveryCode 订单号
     * @return DeliveryOrderPo
     */
    DeliveryOrderPo findByDeliveryCode(String deliveryCode);

    /**
     * 通过订单号查配送单
     *
     * @param orderCode 订单号
     * @return List
     */
    @Query("select o from DeliveryOrderPo o where o.orderCode = ?1 order by o.createTimestamp desc ")
    List<DeliveryOrderPo> findByOrderCode(String orderCode);

    /**
     * 查配送员某些订单的数量
     *
     * @param empCode  配送员编号
     * @param statuses 状态
     * @return Integer
     */
    @Query("select count(1)  from DeliveryOrderPo o where o.deliveryUserNo = ?1  and o.deliveryStatus in ?2")
    Integer findCount(String empCode, Collection<DeliveryOrderStatus> statuses);

    /**
     * 查订单数量
     *
     * @param statuses 状态
     * @return Integer
     */
    @Query("select count(1)  from DeliveryOrderPo o where  o.deliveryStatus in ?1")
    Integer findCount(Collection<DeliveryOrderStatus> statuses);
}